﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Core.Utility.Extensions.Json
{
    /// <summary>
    /// JSON原始属性契约解析器
    /// </summary>
    public class JsonOriginalPropertyContractResolver: DefaultContractResolver
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="member"></param>
        /// <param name="memberSerialization"></param>
        /// <returns></returns>
        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            //var jsonProperty = base.CreateProperty(member, memberSerialization);
            //if (jsonProperty.PropertyType == typeof(DateTime))
            //{
            //    var objType = jsonProperty.DeclaringType;//所在类Type
            //    var property = base.CreateProperty(member, memberSerialization);
            //    return property;
            //}
            //return jsonProperty;
            var property = base.CreateProperty(member, memberSerialization);
            property.PropertyName = member.Name;
            return property;
        }
        
        //protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
        //{
        //    // Let the base class create all the JsonProperties 
        //    IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
        //    // assign the C# property name
        //    foreach (JsonProperty prop in list)
        //    {
        //        //用于去除重复属性的序列化
        //        if (list.Any(p => p.UnderlyingName == prop.UnderlyingName))
        //        {
        //            //原始名称中存在重复的
        //            if (prop.DeclaringType != type)
        //            {
        //                //此属性不在当前type里定义
        //                prop.Ignored = true;
        //            }
        //        }
        //        //prop.PropertyName = prop.UnderlyingName;
        //    }

        //    return list;
        //}
    }
}
